package net.xiduth.game.player.dialogues.impl;

import net.xiduth.game.player.Player;
import net.xiduth.game.player.dialogues.Dialogue;

/**
 * 
 * @author Jae <jae@xiduth.com>
 * 
 * @since <Oct 28, 2013>
 * 
 */
public class LostBob extends Dialogue {

	int npcId;
	int ringId = 15398;

	@Override
	public void start() {
		npcId = (Integer) parameters[0];
		if (player.completedLostRing == true) {
			player.getDialogueManager().startDialogue("SimpleMessage",
					"You have already completed this quest!");
			stage = 4;
		}
		if (player.talkedToBob == true && player.completedLostRing == false) {
			stage = 7;
			sendNPCDialogue(npcId, 9760,
					"Have you found the ring, " + player.getDisplayName() + "?");
		} 
		if (player.talkedToBob == false) {
			sendNPCDialogue(npcId, SCARED, "Help I lost my ring!");
		}
	}

	@Override
	public void run(int interfaceId, int componentId) {
		if (stage == -1) {
			stage = 0;
			sendOptionsDialogue("The Lost Ring", "What's wrong?", "Never mind.");
		} else if (stage == 69) {
			sendNPCDialogue(npcId, SCARED, "Help! I lost my ring!");
			stage = -1;
		} else if (stage == 0) {
			if (componentId == OPTION_1) {
				sendNPCDialogue(npcId, SCARED, "A demon ate my ring!");
				stage = 5;
			}
			if (componentId == OPTION_2) {
				player.getInterfaceManager().closeChatBoxInterface();
				stage = 4;
			}
		} else if (stage == 4) {
			end();
			player.getInterfaceManager().closeChatBoxInterface();

		} else if (stage == 5) {
			stage = 6;
			sendOptionsDialogue("The Lost Ring", "Where can I find him?",
					"I will find your ring.");
		} else if (stage == 6) {
			if (componentId == OPTION_1) {
				sendNPCDialogue(npcId, 9827,
						"The Generals of Xiduth will alert you when he appears again.");
				stage = 5;
			}
			if (componentId == OPTION_2) {
				player.talkedToBob = true;
				sendNPCDialogue(npcId, 9827, "Alright, I believe in you!");
				player.sm("Kortan wants you to collect his ring from the ice demon.");
				player.sm("Quest started: <col=ff000>The Lost Ring</col>.");
				stage = 4;
			}
		} else if (stage == 7) {
			stage = 8;
			sendOptionsDialogue("The Lost Ring", "Yes I have found it!",
					"I'm still looking...");
		} else if (stage == 8) {
			if (componentId == OPTION_1) {
				if (player.getInventory().containsItem(ringId, 1)) {
				sendNPCDialogue(npcId, 9847,
						"Thank you so much, " + player.getDisplayName() + "!");
				player.getInventory().deleteItem(ringId, 1);
				stage = 9;
			} else {
				sendNPCDialogue(npcId, 9764,
						"It doesn't seem like you have the right ring...");
				stage = 4;
			}
				if (componentId == OPTION_2) {
				player.getInterfaceManager().closeChatBoxInterface();
				end();
			}
			}
				
		} else if (stage == 9) {
			player.completedLostRing = true;
			giveRewards(player);
		}
	}

	public static void giveRewards(final Player player) {
		player.completedLostRing = true;
		processItems(player);
		player.getMoneyPouch().add(500000);
		player.getInterfaceManager().closeChatBoxInterface();
		player.getInterfaceManager().sendInterface(275);
		for (int i = 0; i < 310; i++) {
			player.getPackets().sendIComponentText(275, i, "");
			player.getPackets().sendIComponentText(275, 1,
					"You have completed <col=ff000>The Lost Ring</col>!");
			player.getPackets().sendIComponentText(275, 10, "You receive...");
			player.getPackets().sendIComponentText(275, 13,
					"Phoenix teleport scroll x10");
			player.getPackets().sendIComponentText(275, 14, "500,000 coins");
			player.getPackets()
					.sendIComponentText(275, 16,
							"Ability to do the '<col=ff0000>Hidden Temple</col>' quest");
		}
		player.sm("Quest completed: <col=ff000>The Lost Ring</col>.");
	}

	public static void processItems(final Player player) {
		if (player.getInventory().hasFreeSlots()) {
			player.getInventory().addItem(19478, 10);
		} else {
			player.getBank().addItem(19478, 10, true);
		}
	}

	@Override
	public void finish() {

	}

}
